package com.manager.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.manager.pojo.TbUser;

/**
 * 
 * @Title:  LoginInterceptor.java     
 * @Description:    拦截器主要针对admin的页面 用户没有登入的请看下不准访问的页面拦截
 * @Company: www.yangshuai.com
 * @author:      
 * @date:   2018年12月26日 下午9:53:06   
 * @version V1.0
 */
public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// handler执行之前进行处理
		//1判断用户是否登入
		TbUser user = (TbUser)request.getSession().getAttribute("user");
		
		//取不到用户信息
		if(user==null||null == user.getId()) {
			
			//跳转到登入页面
			response.sendRedirect("login");
			//返回false
			return false;
		}
		
		//返回值决定handler是否执行. true:执行,false:不执行
		return true;
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// handler执行之后,返回ModelAndView之前

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// 返回ModelAndView之后
				//响应用户之后

	}

}